<?php
$database = 'dart';
$mysqli = new mysqli("localhost", "root", "", $database);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}

	$res = $mysqli->query("select * from view_all_kw");
	
	print("<form action='test.php'>");
	print("<select onchange='this.form.submit()' name='kw' size='1'>");
	while($row=$res->fetch_assoc()) {
		if($row['kw2'] == $_GET['kw']) {
			print("<option selected value=".$row['kw2'].">".$row['kw']."</option>");
		} else {
			print("<option value=".$row['kw2'].">".$row['kw']."</option>");
		}
	}
	print("</select>");
	print("</form>");
	
	if (isset($_GET['kw']) == true) 
	{
		print("<table border='1'>");
		$res = $mysqli->query("select * from view_score_kw_winner where yearkw=".$_GET['kw']);
		while($row=$res->fetch_assoc()) {
			print("<tr>");
			print("<td>");
			print($row['contraction']);
			print("</td>");
			print("<td>");
			print(round($row['score_abs'],3));
			print("</td>");
			print("<td>");
			print($row['count_games']);
			print("</td>");
			print("</tr>");
		}
		print("</table></br>");
	}
	
	print("<table border='1' cellspacing='0' cellpadding='2'>");
		$kw = "";
		$res = $mysqli->query("select * from view_score_kw_winner order by year desc,kw desc, score_abs desc");
		while($row=$res->fetch_assoc()) {
		if($kw != $row['kw'] && $kw != "") {
				print("</tr></table><table border='1'><tr>");
				$kw = $row['kw'];
			}
			print("<tr>");
			print("<td>");
			print($row['year']);
			print("</td>");
			print("<td>");
			print($row['kw']);
			print("</td>");
			print("<td>");
			print($row['contraction']);
			print("</td>");
			print("<td>");
			print(round($row['score_abs'],3));
			print("</td>");
			print("<td>");
			print($row['count_games']);
			print("</td>");
			print("</tr>");
			
			$kw = $row['kw'];
			
			
		}
		print("</table>");
	
?>